package com.wondersgroup.service.impl;

import com.wondersgroup.entity.Record;
import com.wondersgroup.entity.RecordVo;
import com.wondersgroup.mapper.RecordMapper;
import com.wondersgroup.service.RecordService;
import com.wondersgroup.util.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RecordServiceImpl implements RecordService {


  @Autowired
  private RecordMapper recordMapper;


  @Override
  public Record getDetail(String dwid, String sjh, Date djsj, String type) throws Exception {
    Record record = recordMapper.getDetail(dwid, sjh, djsj);
    // 0 ：明文  1: 密文
    if(record!=null){
      if (type.equals("1")) {
        //脱敏
       record = getTmData(record);
      }
      //加密
     record = getRsaData(record);
    }
    return record;
  }

  @Override
  public int signOut(Record record) {
    return recordMapper.signOut(record);
  }

  @Override
  public List<Record> getRecordByOpenId(String openId, String dwid, Date kssj, Date jssj) {
    return recordMapper.getRecordByOpenId(openId, dwid, kssj, jssj);
  }

  @Override
  public void addRecord(Record record) throws Exception {
    try {
      recordMapper.addRecord(record);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public List<Record> getAllRecords() {
    return recordMapper.getAllRecords();
  }

  @Override
  public List<Record> getRecordsByCompany(String dwid, int rowIndex, int pageSize) throws Exception {
    return recordMapper.getRecordsByCompany(dwid, rowIndex, pageSize);
  }

  @Override
  public List<Record> getCompanyRecords(String dwid) throws Exception {
    return recordMapper.getCompanyRecords(dwid);
  }


  @Override
  // 获取数据后 进行 脱敏 加密
  public List<RecordVo> getRecordsByDate(String dwid, Date kssj, Date jssj, int rowIndex, int pageSize, String xm) throws Exception {
    List<RecordVo> resultList = new ArrayList<>();
    List<Record> recordList = recordMapper.getRecordsByDate(dwid, kssj, jssj, rowIndex, pageSize, xm);
    if (recordList.size() > 0) {
      for (int i = 0; i < recordList.size(); i++) {
        Record record = recordList.get(i);
        RecordVo vo = new RecordVo();
        if(record !=null){
           vo = getReCordVo(record);
           vo = getTmData(vo);
           vo = getRsaData(vo);
        }
        resultList.add(vo);
      }
    }
    return resultList;
  }


  // 脱敏方法
  private Record getTmData(Record record){
    //手机号转换
    if (record.getSjhm() != null && !record.getSjhm().equals("")) {
      String phoneNumber = record.getSjhm().substring(0, 3) + "****" + record.getSjhm().substring(7, record.getSjhm().length());
      record.setSjhm(phoneNumber); // 脱敏
    }
    //姓名转换
    if (record.getXm() != null && !record.getXm().equals("")) {
      String xm = "*" + record.getXm().substring(1, record.getXm().length());
      record.setXm(xm);
    }
    // 身份证号转换
    if (record.getZjhm() != null && !record.getZjhm().equals("")) {
      String zjhm = record.getZjhm().substring(0, 3) + "***********" + record.getZjhm().substring(14, record.getZjhm().length());
      record.setZjhm(zjhm);
    }
    return  record;
  }


  // 加密方法
  private Record getRsaData(Record record) throws  Exception{
    if (record.getSjhm() != null && !record.getSjhm().equals("")) {
      record.setSjhm(RsaUtil.getEncData(record.getSjhm())); //加密
    }
    if (record.getXm() != null && !record.getXm().equals("")) {
      record.setXm(URLEncoder.encode(record.getXm(),"UTF-8"));
      record.setXm(RsaUtil.getEncData(record.getXm()));
    }
    if (record.getZjhm() != null && !record.getZjhm().equals("")) {
      record.setZjhm(RsaUtil.getEncData(record.getZjhm()));
    }
    return record;
  }

  // 转换
  private RecordVo getReCordVo(Record record) {
    RecordVo vo = new RecordVo();
    if (record != null) {
      vo.setId(record.getId());
      vo.setDwid(record.getDwid());
      vo.setOpenId(record.getOpenId());
      vo.setSjhm(record.getSjhm());
      vo.setXm(record.getXm());
      vo.setXb(record.getXb());
      vo.setZjlx(record.getZjlx());
      vo.setZjhm(record.getZjhm());
      vo.setTw(record.getTw());
      vo.setLfsy(record.getLfsy());
      vo.setLfsyqt(record.getLfsyqt());
      vo.setTxrs(record.getTxrs());
      vo.setBz(record.getBz());
      vo.setDjsj(record.getDjsj());
      vo.setQtsj(record.getQtsj());
      vo.setYxbz(record.getYxbz());
      // vo 私有属性
      vo.setRsaXm(record.getXm());
      vo.setRsaSjhm(record.getSjhm());
      vo.setRsaZjhm(record.getZjhm());
    }


    return vo;
  }


  // 脱敏方法
  private RecordVo getTmData(RecordVo vo) {
    //姓名转换
    if (vo.getXm() != null && !vo.getXm().equals("")) {
      String xm = "*" + vo.getXm().substring(1, vo.getXm().length());
      vo.setXm(xm);
    }
    //手机号转换
    if (vo.getSjhm() != null && !vo.getSjhm().equals("")) {
      String phoneNumber = vo.getSjhm().substring(0, 3) + "****" + vo.getSjhm().substring(7, vo.getSjhm().length());
      vo.setSjhm(phoneNumber); // 脱敏
    }
    // 身份证号转换
    if (vo.getZjhm() != null && !vo.getZjhm().equals("")) {
      String zjhm = vo.getZjhm().substring(0, 3) + "***********" + vo.getZjhm().substring(14, vo.getZjhm().length());
      vo.setZjhm(zjhm);
    }
    return vo;
  }


  // 加密方法
  private RecordVo getRsaData(RecordVo vo) throws Exception {
    if (vo.getRsaXm() != null && !vo.getRsaXm().equals("")) {
      vo.setRsaXm(URLEncoder.encode(vo.getRsaXm(), "UTF-8" ));
      vo.setRsaXm(RsaUtil.getEncData(vo.getRsaXm()));
    }
    if (vo.getRsaSjhm() != null && !vo.getRsaSjhm().equals("")) {
      vo.setRsaSjhm(RsaUtil.getEncData(vo.getRsaSjhm()));
    }
    if (vo.getRsaZjhm() != null && !vo.getRsaZjhm().equals("")) {
      vo.setRsaZjhm(RsaUtil.getEncData(vo.getRsaZjhm()));
    }
    return vo;
  }

}
